package com.corpgovernment.permission.service;


import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

public interface RelationSrv<L, R> {
    void clearLeft(Set<L> ids);

    void clearRight(Set<R> ids);

    void batchAssociate(Set<L> leftIds, Set<R> rightIds);

    default List<Relation<L, R>> map(Set<L> leftIds, Set<R> rightIds) {
	    if (CollectionUtils.isEmpty(leftIds) || CollectionUtils.isEmpty(rightIds)) {
		    return Collections.emptyList();
	    }
        List<Relation<L, R>> rList = new ArrayList<>();
        leftIds.stream().filter(Objects::nonNull).forEach(
                l -> rList.addAll(rightIds.stream()
                        .filter(Objects::nonNull)
                        .map(r -> new Relation<>(l, r))
                        .collect(Collectors.toList()))
        );
        return rList;
    }

    @Data
    @AllArgsConstructor
    class Relation<Left, Right> {
        Left left;
        Right right;
    }
}
